package de.koller.aframe.plain;

import de.koller.oceanFramework.tree.Sea3X;
import java.awt.Canvas;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class InputPlain extends Sea3X {

	private Set<Integer> keyPressed = new TreeSet<>();
	private Set<Integer> keyTyped = new TreeSet<>();
	
	private Set<Integer> buttonPressed = new TreeSet<>();
	private Set<Integer> buttonTyped = new TreeSet<>();
	
	private List<Character> charPressed = new ArrayList<>();
	
	private int mouseX;
	private int mouseY;
	private double mouseWheel;
	
	private int mouseLastX;
	private int mouseLastY;
	
	public InputPlain( Canvas canvas ) {
		canvas.addKeyListener( new InputPlain.InputReceiver() );
		
		InputPlain.MouseInputReceiver mir = new InputPlain.MouseInputReceiver();
		canvas.addMouseListener( mir );
		canvas.addMouseMotionListener( mir );
		canvas.addMouseWheelListener( mir );
	}

	@Override
	public void postChildUpdate( int elapsedTime ) {
		super.postChildUpdate( elapsedTime );
		
		keyTyped.clear();
		buttonTyped.clear();
		charPressed.clear();
		mouseWheel = 0;
		
		mouseLastX = mouseX;
		mouseLastY = mouseY;
	}

	public int getMouseDX() {
		return mouseX - mouseLastX;
	}
	
	public int getMouseDY() {
		return mouseY - mouseLastY;
	}
	
	public boolean isKeyDown( int key ) {
		return keyPressed.contains( key );
	}

	public boolean isKeyPressed( int key ) {
		return keyTyped.contains( key );
	}
	
	public boolean isMouseDown( int button ) {
		return buttonPressed.contains( button );
	}

	public boolean isMousePressed( int button ) {
		return buttonTyped.contains( button );
	}

	public int getMouseX() {
		return mouseX;
	}

	public int getMouseY() {
		return mouseY;
	}

	public double getMouseWheel() {
		return mouseWheel;
	}

	public List<Character> getCharPressed() {
		return charPressed;
	}
	
	private class MouseInputReceiver extends MouseAdapter {
		
		@Override
		public void mouseDragged( MouseEvent e ) {
			super.mouseDragged( e );
			mouseX = e.getX();
			mouseY = e.getY();
		}

		@Override
		public void mouseMoved( MouseEvent e ) {
			super.mouseMoved( e );
			mouseX = e.getX();
			mouseY = e.getY();
		}

		@Override
		public void mousePressed( MouseEvent e ) {
			super.mousePressed( e );
			buttonPressed.add( e.getButton() );
		}

		@Override
		public void mouseReleased( MouseEvent e ) {
			super.mouseReleased( e );
			buttonPressed.remove( e.getButton() );
			buttonTyped.add( e.getButton() );
		}

		@Override
		public void mouseWheelMoved( MouseWheelEvent e ) {
			super.mouseWheelMoved( e );
			
			mouseWheel = e.getPreciseWheelRotation();
		}
		
	}
	
	private class InputReceiver extends KeyAdapter {

		@Override
		public void keyPressed( KeyEvent e ) {
			super.keyPressed( e );
			keyPressed.add( e.getKeyCode() );
			
			char c = e.getKeyChar();
			if( Character.isDefined( c ) )
				charPressed.add( e.getKeyChar() );
		}

		@Override
		public void keyReleased( KeyEvent e ) {
			super.keyReleased( e );
			keyPressed.remove( e.getKeyCode() );
			keyTyped.add( e.getKeyCode() );
		}

	}
	
}
